package com.wsoft.bpm.listener.execute;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.wsoft.bpm.constants.BpmConstant;
import com.wsoft.bpm.entity.ActNoticeEntity;
import com.wsoft.bpm.service.IActNoticeService;
import com.wsoft.constant.CommonConstant;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 增加抄送监听器
 *
 * @author zhonglj
 * @date 2025/2/13 15:49
 **/
@Component("addNotificationListener")
public class AddNotificationListener implements ExecutionListener {
    @Resource
    private IActNoticeService actNoticeService;

    @Override
    public void notify(DelegateExecution execution) {
        // 获取抄送人员流程变量
        List<String> noticeList = execution.getVariable(BpmConstant.Hr.VARIABLE_KEY_PERFORMANCE_NOTICE_LIST, List.class);
        if (CollUtil.isNotEmpty(noticeList)) {
            actNoticeService.saveBatch(noticeList.stream().map(e -> {
                ActNoticeEntity notice = new ActNoticeEntity();
                notice.setAssign(StrUtil.toString(e));
                notice.setIsRead(CommonConstant.YESNO_NO);
                notice.setProcessInstanceId(execution.getProcessInstanceId());
                notice.setBusinessCode(execution.getProcessInstanceBusinessKey());
                return notice;
            }).collect(Collectors.toList()));
        }
    }

}
